package com.dinzeer.sjapadder.sa;

import com.dinzeer.legendreliclib.lib.util.WaitingTick;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.sjapadder.Config;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.entity.EntityJudgementCut;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.AABB;

import java.util.List;
import java.util.Random;

public class DarkCut {





    public static void summonJudgementCutEX(LivingEntity playerIn) {
        // 获取玩家周围5格内的所有实体（排除玩家自身）
        AABB area = new AABB(
                (playerIn.getX() - 5.0),
                (playerIn.getY() - 2.5),
                (playerIn.getZ() - 5.0),
                (playerIn.getX() + 5.0),
                (playerIn.getY() + 2.5),
                (playerIn.getZ() + 5.0)
        );

        List<Entity> entities = playerIn.level().getEntities(playerIn, area);
        entities.remove(playerIn); // 确保排除玩家自身

        Random rand = new Random();
        int entityCount = entities.size();
        int totalCount = Config.countDarkCutJudgementCutEx;


        for (int i = 0; i < Math.min(entityCount, totalCount); i++) {
            int finalI = i;
            for (int j = 0; j < 3; j++){
            WaitingTick.schedule(i, () -> {
                Entity target = entities.get(finalI);
                createJudgementCut(
                        playerIn,
                        target.getX(),
                        target.getY() + 1,
                        target.getZ(),
                        rand
                );
            });
            }
        }


    }




    public static void summonJudgementCut(LivingEntity playerIn) {
        // 获取玩家周围5格内的所有实体（排除玩家自身）
        AABB area = new AABB(
                                (playerIn.getX() - 5.0),
                                (playerIn.getY() - 2.5),
                                (playerIn.getZ() - 5.0),
                                (playerIn.getX() + 5.0),
                                (playerIn.getY() + 2.5),
                                (playerIn.getZ() + 5.0)
        );

        List<Entity> entities = playerIn.level().getEntities(playerIn, area);
        entities.remove(playerIn); // 确保排除玩家自身

        Random rand = new Random();
        int entityCount = entities.size();
        int totalCount = Config.countDarkCutJudgementCut;


        for (int i = 0; i < Math.min(entityCount, totalCount); i++) {
            int finalI = i;
            WaitingTick.schedule(i, () -> {
                Entity target = entities.get(finalI);
                createJudgementCut(
                        playerIn,
                        target.getX(),
                        target.getY() + 1,
                        target.getZ(),
                        rand
                );
            });
        }


    }

    private static void createJudgementCut(LivingEntity owner, double x, double y, double z, Random rand) {
        if (owner.getMainHandItem().getItem() instanceof ItemSlashBlade){
            // 添加随机偏移使效果更自然
            double offsetX = (rand.nextDouble() - 0.5) +1;
            double offsetY = (rand.nextDouble() - 0.5) +1;
            double offsetZ = (rand.nextDouble() - 0.5) +1;

            EntityJudgementCut judgementCut = new EntityJudgementCut(
                    SlashBlade.RegistryEvents.JudgementCut,
                    owner.level()
            );

            judgementCut.setPos(
                    x + offsetX,
                    y + offsetY,
                    z + offsetZ
            );

            judgementCut.setIsCritical(true);
            judgementCut.setDamage(SlashBladeUtil.getBaseAttackModifier(owner.getMainHandItem())*0.1);
            judgementCut.setColor(SlashBladeUtil.getColorCode(owner.getMainHandItem()));
            judgementCut.setOwner(owner);
            judgementCut.setCycleHit(true);
            owner.level().addFreshEntity(judgementCut);
        }
    }
}
